package com.evasion.filtro;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
//import com.itcuties.examples.webapps.filterlogin.beans.LoginBean;
 
/**
 * Filter checks if LoginBean has loginIn property set to true.
 * If it is not set then request is being redirected to the login.xhml page.
 *
 * @author itcuties
 *
 */
public class LoginFilter implements Filter {
 
    /**
     * Checks if user is logged in. If not it redirects to the login.xhtml page.
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // Get the loginBean from session attribute
       // LoginBean loginBean = (LoginBean)((HttpServletRequest)request).getSession().getAttribute("loginBean");
    //	ControladorLogin loginBean = (ControladorLogin)((HttpServletRequest)request).getSession().getAttribute("valor");
    	//System.out.println("Usuario que se quiere l: "+loginBean.getUsuario());
        // For the first application request there is no loginBean in the session so user needs to log in
        // For other requests loginBean is present but we need to check if user has logged in successfully
      // if (loginBean == null) {
              
          //  String contextPath = ((HttpServletRequest)request).getContextPath();
           // System.out.println("Valor del contexto: "+contextPath);
           // ((HttpServletResponse)response).sendRedirect(contextPath + "/seguridad/login.xhtml");
           // return;
       // }
        chain.doFilter(request, response);
             
    }
 
    public void init(FilterConfig config) throws ServletException {
    	//Get init parameter
        String testParam = config.getInitParameter("test-param");
         
        //Print the init parameter
        System.out.println("Test Param: " + testParam);
    }
 
    public void destroy() {
        // Nothing to do here!
    }  
     
}